<?php

namespace app\controller;

use app\BaseController;
use app\model\CourseWare;
use app\model\SchoolCate;
use think\db\Query;
use think\Response;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Slide;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;

class Index extends BaseController
{
    protected $aliyunHost = 'https://beilejihua.oss-cn-shenzhen.aliyuncs.com';
    protected $aliyunParams = '?x-oss-process=image/format,jpg/quality,q_60';

    public function success($data): Response
    {
        return Response::create([
            'code' => 200,
            'data' => $data,
            'message' => 'success',
        ], 'json')->header(['Content-Type' => 'application/json']);
    }

    public function fail($message): Response
    {
        return Response::create([
            'code' => 500,
            'message' => $message,
        ], 'json')->header(['Content-Type' => 'application/json']);
    }

    public function index()
    {
        $category = app()
            ->make(SchoolCate::class)
            ->with(['children'])
            ->where(['status' => 1, 'pid' => 0])
            ->order('sort desc, id desc')
            ->select();

        return $this->success($category);
    }

    public function getList()
    {
        // $page = request()->get('page', 1);
        // $limit = request()->get('limit', 10);
        $pid = request()->get('pid', 0);
        $cid = request()->get('cid', 0);

        $list = app()
            ->make(CourseWare::class)
            ->when($cid == 0, function (Query $query) use ($pid) {
                $pids = app()->make(SchoolCate::class)->where('pid', $pid)->column('id');
                $query->whereIn('pid', $pids);
            })
            ->when($cid > 0, function (Query $query) use ($cid) {
                $query->whereIn('pid', $cid);
            })
            // ->page($page, $limit)
            ->select()
            ->each(function ($item) {
                $item['thumb'] = $this->aliyunHost . '/' . $item['thumb'];
                return $item;
            });

        return $this->success($list);
    }

    public function getInfo($id)
    {
        $info = app()
            ->make(CourseWare::class)
            ->with(['file.list'])
            ->where('id', $id)
            ->find();
        if ($info['file']) {
            foreach ($info['file'] as &$v) {
                $v['filepath'] = $this->aliyunHost . '/' . $v['filepath'];
                if ($v['list']) {
                    foreach ($v['list'] as &$vv) {
                        $vv['filepath'] = $this->aliyunHost . '/' . $vv['filepath'];
                    }
                }
            }
        }

        return $this->success($info);
    }

    public function pdf2ppt()
    {
        $pdfPath = app()->getRootPath() . 'public/pdf_clear_water/nwc27eZAth.pdf';
        $outputDir = app()->getRootPath() . 'public/pdf_clear_water/output';
        $pptPath = $outputDir . app()->getRootPath() . 'public/pdf_clear_water/presentation.pptx';

        // 确保输出目录存在
        if (!file_exists($outputDir)) {
            mkdir($outputDir, 0777, true);
        }

        // 使用ImageMagick将PDF转换为图片
        $imagePaths = [];
        exec("convert -density 150 '{$pdfPath}' -quality 100 '{$outputDir}/slide_%d.jpg'", $output, $returnVar);
        if ($returnVar === 0) {
            $imagePaths = glob("{$outputDir}/slide_*.jpg");
        } else {
            throw new \Exception("Error converting PDF to images");
        }

        return 'success';
    }
}
